package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.DetailedTravel_area_Modles;

/**
 * Created by lanouhn on 16/8/31.
 */
public class Detailed_Travel_Area_Adapter extends RecyclerView.Adapter<Detailed_Travel_Area_Adapter.TravelViewHolder> {

    private Context context;
    private DetailedTravel_area_Modles modles;
    private int count = 0;
    private TravelAreaShowContentAdaper adapter;
    private boolean isFirst = true;

    public Detailed_Travel_Area_Adapter(Context context, DetailedTravel_area_Modles modles) {
        this.context = context;
        this.modles = modles;
        this.count += modles.getAttraction_trip_tags().size() + 1;
    }

    @Override
    public TravelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_detailed_travel_area, null);
        TravelViewHolder holder = new TravelViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final TravelViewHolder holder, int position) {
        if (position == 0) {
            holder.tvItemDetailedTravelTitle.setText("实用小贴士");

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isFirst) {
                        holder.linImageDetailedTravel.setVisibility(View.VISIBLE);
                        final WebView webView = new WebView(context);
                        webView.loadDataWithBaseURL(null, modles.getTips_html(), "text/html", "UTF-8", null);
                        webView.getSettings().setJavaScriptEnabled(true);

                        webView.reload();
                        webView.setWebViewClient(new WebViewClient() {
                            @Override
                            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                webView.loadDataWithBaseURL(null, modles.getTips_html(), "text/html", "UTF-8", null);
                                return true;
                            }
                        });
                        holder.linImageDetailedTravel.addView(webView);
                        isFirst = false;
                    }
                }
            });

        } else {
            int number = position - 1;
            holder.linImageDetailedTravel.setVisibility(View.VISIBLE);
            holder.tvItemDetailedTravelTitle.setText(modles.getAttraction_trip_tags().get(number).getName());


            RecyclerView recyclerView = new RecyclerView(context);

            adapter = new TravelAreaShowContentAdaper(context, modles.getAttraction_trip_tags().get(number), modles.getName_zh_cn());
            recyclerView.setAdapter(adapter);

            LinearLayoutManager lineManager = new LinearLayoutManager(context);
            lineManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(lineManager);


            holder.linImageDetailedTravel.addView(recyclerView);
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class TravelViewHolder extends RecyclerView.ViewHolder {

        private TextView tvItemDetailedTravelTitle;
        private LinearLayout linImageDetailedTravel;

        public TravelViewHolder(View itemView) {
            super(itemView);
            tvItemDetailedTravelTitle = (TextView) itemView.findViewById(R.id.tv_item_detailed_travel_title);
            linImageDetailedTravel = (LinearLayout) itemView.findViewById(R.id.lin_image_detailed_travel);
        }
    }
}
